package com.guigu.servicesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.common_util.pojo.utils.Result;
import com.guigu.servicesystem.entity.TbAdmin;
import com.guigu.servicesystem.entity.TbAdminRole;
import com.guigu.servicesystem.entity.vo.Admin_role;
import com.guigu.servicesystem.mapper.TbAdminMapper;
import com.guigu.servicesystem.service.TbAdminRoleService;
import com.guigu.servicesystem.service.TbAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@Service
public class TbAdminServiceImpl extends ServiceImpl<TbAdminMapper, TbAdmin> implements TbAdminService {
    @Autowired
    private TbAdminRoleService tbAdminRoleService;

    @Override
    public Result findPage(Map<String, Object> searchMap, Integer page, Integer size) {
        QueryWrapper<TbAdmin> wrapper = createExample(searchMap);
        //创建page对象
        Page<TbAdmin> pageBrand = new Page<>(page,size);
        baseMapper.selectPage(pageBrand,wrapper);
        return Result.ok().data("tbAdminRow",pageBrand.getRecords()).data("total",pageBrand.getTotal());
    }

    @Override
    public List<Admin_role> selectAllAdmin_Role(int status) {
        return baseMapper.selectAllAdmin_Role(status);
    }

    @Override
    public Admin_role findbyAdmin(int id) {
        return baseMapper.findbyAdmin(id);
    }
    @Transactional
    @Override
    public void insert(Admin_role addForm) {
        TbAdmin admin=new TbAdmin();
        admin.setLoginName(addForm.getLoginName());
        admin.setPassword(addForm.getPassword());
        admin.setStatus("1");
        baseMapper.insert(admin);
        TbAdminRole adminRole=new TbAdminRole();
        adminRole.setAdminId(admin.getId());
        adminRole.setRoleId(addForm.getRoleId());
        tbAdminRoleService.save(adminRole);
    }
    @Transactional
    @Override
    public void updataAdmin(Admin_role admin_role) {
        TbAdmin admin=new TbAdmin();
        admin.setId(admin_role.getId());
        admin.setLoginName(admin_role.getLoginName());
        admin.setPassword(admin_role.getPassword());
        admin.setStatus(admin_role.getStatus());
        baseMapper.updateById(admin);
        TbAdminRole tbAdminRole=new TbAdminRole();
        tbAdminRole.setAdminId(admin_role.getId());
        tbAdminRole.setRoleId(admin_role.getRoleId());
        tbAdminRoleService.updateById(tbAdminRole);
    }


    // 根据查询条件封装模块对象
    private QueryWrapper<TbAdmin> createExample(Map<String, Object> searchMap){
        QueryWrapper<TbAdmin> wrapper = new QueryWrapper<>();
        if(searchMap!=null) {
            if(searchMap.get("id")!=null)
                wrapper.eq("id",searchMap.get("id"));
            if(searchMap.get("login_name")!=null && !"".equals(searchMap.get("login_name")))
                wrapper.like("login_name",searchMap.get("login_name"));
        }
        return wrapper;
    }

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        System.out.println("进入了查询方法+++++++++++++");
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("login_name",s);
        UserDetails u = baseMapper.selectOne(queryWrapper);
        System.out.println(u);
        return u;
    }
}
